﻿using Coldairarrow.Entity.apifox;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Text;

namespace Coldairarrow.Util.Helper
{
    public class JsonOperateService
    {
        public static OpenApiDocument ReadSwagger()
        {
            string Identityfilename = @"/swagger.json";
            var directorypath = Directory.GetCurrentDirectory();
            string modelPath = directorypath + Identityfilename;
            using FileStream stream = File.OpenRead(modelPath);
            var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic1);
            return openApiDoc;
        }
        public static string ReadSwaggerJson()
        {
            string Identityfilename = @"/swagger3.0.json";
            var directorypath = Directory.GetCurrentDirectory();
            string modelPath = directorypath + Identityfilename;
            var Json = GetJsonFile(modelPath);
            //JObject jt = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(Json);
            

            //object jtpaths = jt["paths"].ToString();
            //jtpaths["key"]
            return Json;
        }

    
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        public static void WriteMyTypeToEntityFile(string name, string code)
        {
            string path = @"D:\TestNSwag\Coldairarrow.Entity";
            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);

            }
            var filepath = Path.Combine(path, $"{name}.cs");

            using (StreamWriter writer = File.CreateText(filepath))
            {

                writer.Write(code);

            }
        }
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        public static void WriteMyTypeToFile(string name,string code)
        {
            string path = @"D:\TestNSwag\Coldairarrow.Api\";
            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);

            }
            var filepath = Path.Combine(path, $"{name}.cs");

            using (StreamWriter writer = File.CreateText(filepath))
            {

                writer.Write(code);

            }
        }
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        public static void WriteMyTypeToFile(string name,string typename, string code)
        {
            string path = @"D:\TestNSwag\"+typename;
            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);

            }
            var filepath = Path.Combine(path, $"{name}.cs");

            using (StreamWriter writer = File.CreateText(filepath))
            {

                writer.Write(code);

            }
        }
        public static string FullJsonUrl (string url)
        {
            var directorypath = Directory.GetCurrentDirectory();
            string FullPath = directorypath + url;
            return FullPath;
        }


        //获取到本地的Json文件并且解析返回对应的json字符串
        public static string GetJsonFile(string filepath)
        {
            string json = string.Empty;
            using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312")))
                {
                    json = sr.ReadToEnd().ToString();
                }
            }
            return json;
        }
  
    }
}
